<!--
 * @Description: 选择图片的窗口
 * @Date: 2023-04-04 19:48:48
 * @LastEditTime: 2023-04-10 15:10:30
 * @Author: Black
 * @FilePath: \store_admin\src\components\ChooseImage.vue
-->
<template>
  <div v-if="modelValue && preview">
    <el-image v-if="typeof modelValue == 'string'" :src="modelValue" fit="cover"
      class="w-[100px] h-[100px] rounded border mr-2"></el-image>
    <div v-else class="flex flex-wrap">
      <div class="relative mx-1 mb-2 w-[100px] h-[100px]" v-for="(url, index) in modelValue" :key="index">
        <el-icon class="absolute right-[5px] top-[5px] cursor-pointer bg-white rounded-full" style="z-index: 10;"
          @click="removeImage(url)">
          <CircleClose />
        </el-icon>
        <el-image :src="url" fit="cover" class="w-[100px] h-[100px] rounded border mr-2"></el-image>
      </div>
    </div>
  </div>

  <div v-if="preview" class="choose_image_btn" @click="open">
    <el-icon :size="25" class="text-gray-500">
      <Plus />
    </el-icon>
  </div>

  <el-dialog title="选择图片" v-model="dialogVisible" width="75%" top="5vh">
    <el-container class="bg-white rounded-lg" style="height: 70vh ">
      <!-- <el-header class="image-header">
        <el-button class="btns1" type="primary" size="small" @click="handleOpenCreate">新增分类</el-button>
        <el-button class="btns2" type="warning" size="small" @click="handleOpenUpload">上传图片</el-button>
      </el-header> -->
      <el-container>
        <ImageMain openChoose :limit="limit" ref="ImageMainRef" @choose="handleChoose"></ImageMain>
        <ImageAside ref="ImageAsideRef" @change="handleAsideChange" @upload="handleOpenUpload"></ImageAside>
      </el-container>
    </el-container>
    <template #footer>
      <span>
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import ImageMain from '~/components/ImageMain.vue'
import ImageAside from '~/components/ImageAside.vue'
import { ref } from 'vue'
import { toast } from "../composables/util"

const dialogVisible = ref(false)

const callbackFunction = ref(null)
const open = (callback = null) => {
  callbackFunction.value = callback
  dialogVisible.value = true
}
const close = () => dialogVisible.value = false

// const handleOpenCreate = () => ImageAsideRef.value.handleCreate()

const ImageMainRef = ref(null)

const handleAsideChange = (image_class_id) => {
  ImageMainRef.value.loadData(image_class_id)
}

const handleOpenUpload = () => ImageMainRef.value.openUploadFile()

const props = defineProps({
  modelValue: [String, Array],
  limit: {
    type: Number,
    default: 1
  },
  preview: {
    type: Boolean,
    default: true
  }
})
const emit = defineEmits(["update: modelValue"])

let urls = []
const handleChoose = (e) => {
  urls = e.map(o => o.url)
}

const submit = () => {
  let value = []
  // 根据选择图片的张数执行不同操作
  if (props.limit == 1) {
    value = urls[0]
  } else {
    value = props.preview ? [...props.modelValue, ...urls] : [...urls]
    if (value.length > props.limit) {
      let limit = props.preview ? (props.limit - props.modelValue.length) : props.limit
      return toast("最多还能选择" + limit + "张", "warning")
    }
  }
  if (value && props.preview) {
    emit("update:modelValue", value)
  }
  if (!props.preview && typeof callbackFunction.value === "function") {
    callbackFunction.value(value)
  }
  close()
}

const removeImage = (url) => emit("update:modelValue", props.modelValue.filter(u => u != url))

defineExpose({
  open
})

</script>

<style scoped>
.image-header {
  border-bottom: 1px solid #eeeeee;
  @apply flex items-center;
}

.choose_image_btn {
  @apply w-[100px] h-[100px] rounded border flex justify-center items-center cursor-pointer hover: (bg-gray-100);
}

.btns1 {
  @apply bg-violet-400;
  height: 38px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
}

.btns2 {
  @apply bg-orange-400;
  height: 38px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
}
</style>